package com.cw.jeeyt.service.impl.security;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.security.SecurityOrgUserBo;
import com.cw.jeeyt.dao.intf.security.SecurityOrgDao;
import com.cw.jeeyt.dao.intf.security.SecurityOrgUserDao;
import com.cw.jeeyt.dao.intf.security.SecurityUserDao;
import com.cw.jeeyt.dao.model.security.SecurityOrgDo;
import com.cw.jeeyt.dao.model.security.SecurityOrgUserDo;
import com.cw.jeeyt.dao.model.security.SecurityUserDo;
import com.cw.jeeyt.dao.query.security.SecurityOrgQuery;
import com.cw.jeeyt.dao.query.security.SecurityUserQuery;
import com.cw.jeeyt.service.converter.security.SecurityOrgUserConverter;
import com.cw.jeeyt.service.intf.security.SecurityOrgUserService;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 机构用户信息表业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class SecurityOrgUserServiceImpl implements SecurityOrgUserService {

    private static final Logger logger = LoggerFactory.getLogger(SecurityOrgUserServiceImpl.class);
    @Autowired
    private SecurityOrgUserDao securityOrgUserDao;
    @Autowired
    private SecurityUserDao securityUserDao;
    @Autowired
    private SecurityOrgDao securityOrgDao;

    /**
     * 分页查询机构用户信息表
     *
     * @param securityOrgUserBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SecurityOrgUserBo> queryByPage(SecurityOrgUserBo securityOrgUserBo, Integer startNum,
        Integer pageSize) throws Exception {
        PageInfo<SecurityOrgUserDo> pageInfo = securityOrgUserDao.queryByPage(
            SecurityOrgUserConverter.bo2Do(securityOrgUserBo), startNum, pageSize);
        List<SecurityOrgUserBo> securityOrgUserBos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(pageInfo.getList())) {
            securityOrgUserBos = SecurityOrgUserConverter.doList2BoList(pageInfo.getList());
            //获取分页列表用户id集合以及机构id集合
            List<Long> userIds = pageInfo.getList()
                .stream()
                .map(securityOrgUserDo -> securityOrgUserDo.getUserId())
                .collect(Collectors.toList());
            List<Long> orgIds = pageInfo.getList()
                .stream()
                .map(securityOrgUserDo -> securityOrgUserDo.getOrgId())
                .collect(Collectors.toList());
            List<SecurityUserDo> userDos = securityUserDao.selectByExample(SecurityUserQuery.builder()
                .ids(userIds)
                .build());
            List<SecurityOrgDo> orgDos = securityOrgDao.selectByExample(SecurityOrgQuery.builder()
                .orgIds(orgIds)
                .build());
            Map<Long, SecurityUserDo> userMap = userDos.stream()
                .collect(Collectors.toMap(securityUserDo -> securityUserDo.getId(), securityUserDo -> securityUserDo));
            Map<Long, SecurityOrgDo> orgMap = orgDos.stream()
                .collect(Collectors.toMap(securityOrgDo -> securityOrgDo.getOrgId(), securityOrgDo -> securityOrgDo));
            for (SecurityOrgUserBo orgUserBo : securityOrgUserBos) {
                if (userMap.containsKey(orgUserBo.getUserId())) {
                    orgUserBo.setUserName(userMap.get(orgUserBo.getUserId())
                        .getUsername());
                }
                if (orgMap.containsKey(orgUserBo.getOrgId())) {
                    orgUserBo.setOrgName(orgMap.get(orgUserBo.getOrgId())
                        .getOrgName());
                }
            }
        }
        return new PageInfo<>(pageInfo, securityOrgUserBos);
    }

    /**
     * 根据主键查寻机构用户信息表信息
     *
     * @param id
     * @return
     */
    @Override
    public SecurityOrgUserBo getSecurityOrgUserById(String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            logger.error("--主键id为空--");
            return null;
        }
        return SecurityOrgUserConverter.do2Bo(securityOrgUserDao.getById(id));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importSecurityOrgUser(String userIds, String orgId) throws Exception {
        if (StringUtils.isEmpty(userIds)) {
            logger.error("用户id为空");
            return;
        }
        String[] userIdList = userIds.split(",");
        for (String userId : userIdList) {
            SecurityOrgUserDo securityOrgUser = new SecurityOrgUserDo();
            securityOrgUser.setId(SequenceTool.nextId());
            securityOrgUser.setUserId(Long.parseLong(userId));
            securityOrgUser.setOrgId(Long.parseLong(orgId));
            SecurityOrgUserDo checkOrgUser = securityOrgUserDao.checkOrgUser(Long.parseLong(userId),
                Long.parseLong(orgId));
            if (checkOrgUser == null) {
                securityOrgUserDao.insert(securityOrgUser);
            }
        }
    }

    /**
     * 新增机构用户信息表
     *
     * @param securityOrgUserBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(SecurityOrgUserBo securityOrgUserBo) throws Exception {

        if (securityOrgUserBo == null) {
            logger.error("机构用户信息表为空");
            return null;
        }
        SecurityOrgUserDo securityOrgUser = SecurityOrgUserConverter.bo2Do(securityOrgUserBo);
        securityOrgUser.setId(SequenceTool.nextId());
        return securityOrgUserDao.insert(securityOrgUser);
    }

    /**
     * 更新机构用户信息表信息
     *
     * @param securityOrgUserBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SecurityOrgUserBo securityOrgUserBo) throws Exception {

        if (securityOrgUserBo == null) {
            logger.error("机构用户信息表为空");
            return;
        }

        securityOrgUserDao.update(SecurityOrgUserConverter.bo2Do(securityOrgUserBo));
    }

    /**
     * 删除机构用户信息表
     *
     * @param securityOrgUserBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SecurityOrgUserBo securityOrgUserBo) throws Exception {

        if (securityOrgUserBo == null) {
            logger.error("机构用户信息表为空");
            return;
        }
        securityOrgUserDao.delete(SecurityOrgUserConverter.bo2Do(securityOrgUserBo));
    }
}
